#include "ifmov.hpp"
#include <pass_manager.h>
#include <rtl.h>
#include <context.h>
#include <memmodel.h>
#include <emit-rtl.h>
const pass_data pass_data_sw_64_slave_ifmov =
  {
    RTL_PASS,
    "slave_ifmov",
    OPTGROUP_NONE,
    TV_NONE,
    0,
    0,
    0,
    0,
    TODO_df_finish
  };
//fcpys a,a,a
class pass_sw_64_slave_ifmov : public rtl_opt_pass {
public:
  pass_sw_64_slave_ifmov(gcc::context *ctx): rtl_opt_pass(pass_data_sw_64_slave_ifmov, ctx){}
  
  virtual bool gate (function *){
    return true;
  }
  virtual unsigned int execute (function *f) {
    rtx_insn *insn;
    for (insn = get_insns (); insn; insn = NEXT_INSN (insn)){

      if (NONJUMP_INSN_P(insn) && NOTE_KIND(insn) != NOTE_INSN_DELETED){
        rtx pat = PATTERN(insn);
        if (GET_CODE(pat) == SET && GET_CODE(XEXP(pat, 1)) == UNSPEC && XINT(XEXP(pat, 1), 1) == UNSPEC_IFMOV){
          rtx lhs = XEXP(pat, 0);
          rtx rhs = XVECEXP(XEXP(pat, 1), 0, 0);
          if (REG_P(lhs) && REG_P(rhs) && REGNO(lhs) == REGNO(rhs)){
            SET_INSN_DELETED(insn);
          }
        }
      }
    }
    return 0;
  }
};

void sw_64_slave_ifmov::add_passes(){
  if (TARGET_SW_SLAVE){
    nohp::register_pass(new pass_sw_64_slave_ifmov(g), "sched2", 1, PASS_POS_INSERT_BEFORE);
  }
}
